
package com.zyht.bank.wechat.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.wx.entity.WxFansUser;
import com.wx.model.dictionary.Constant;
import com.wx.util.PhoneUtils;
import com.wx.util.StringUtils;
import com.zyht.bank.entity.AxdCustInfo;
import com.zyht.bank.entity.AxdCustInfoExample;
import com.zyht.bank.service.AxdCustInfoService;
import com.zyht.bank.wechat.intercepter.Binding;
import com.zyht.bank.wechat.utils.idcard.HttpClientUtils;
import com.zyht.common.bean.PropertiesUtilBean.PropertiesUtils;
import com.zyht.redis.service.RedisService;
import com.zyht.wechat.framework.Oauth;
import com.zyht.wechat.framework.constant.ConfigConstants;

/**
 * 
 * @author qjl
 * @data 2017年7月5日
 */
@Controller
@RequestMapping("/anon/cust")
public class WechatCustInfoController {

	@Resource
	private AxdCustInfoService axdCustInfoService;

	@Resource
	private RedisService redisService;

	private String BASEPATH = "/banks/wechat/user/";

	/**
	 * 个人中心
	 * 
	 * @param model
	 * @return
	 */
	@Oauth
	@Binding
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(Model model, HttpSession session) {
		// 获取绑定信息
		AxdCustInfo custInfo = (AxdCustInfo) session.getAttribute(Constant.CURRENT_USER_BINDINFO_FROM_EVENT_VIEW);
		// 脱敏处理
		StringBuilder idnum = (new StringBuilder(custInfo.getCustIdNum()));
		idnum.replace(6, idnum.length() - 4, "****");
		StringBuilder phone = (new StringBuilder(custInfo.getCustPhone()));
		phone.replace(3, phone.length() - 3, "****");
		custInfo.setCustIdNum(idnum.toString());
		custInfo.setCustPhone(phone.toString());
		// 获取fans信息
		WxFansUser wxFansUser = (WxFansUser) session.getAttribute(Constant.CURRENT_USER_FANINFO_FROM_EVENT_VIEW);
		model.addAttribute("custInfo", custInfo);
		model.addAttribute("wxFansUser", wxFansUser);
		return makeView("userindex");
	}

	/**
	 * 编辑页面
	 * 
	 * @param model
	 * @param axdCustInfo
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "tobind", method = RequestMethod.GET)
	public String tobind(Model model, HttpSession session) {
		// 获取用户
		AxdCustInfo custInfo = (AxdCustInfo) session.getAttribute(Constant.CURRENT_USER_BINDINFO_FROM_EVENT_VIEW);
		if (custInfo != null && custInfo.getBandingStatus().equals("1")) {
			model.addAttribute("status", "1");
		}
		return makeView("bind");
	}

	/**
	 * 绑定
	 * 
	 * @param model
	 * @param axdCustInfo
	 * @param session
	 * @return
	 */
	@Oauth
	@RequestMapping(value = "binding", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> bind(Model model, String cradId, String custName, String phone, String smscode,
			HttpSession session) {
		Map<String, String> map = new HashMap<String, String>();
		// 短信验证码
		if (!PhoneUtils.isMobile(phone)) {
			map.put("state", "fail");
			map.put("msg", "非法手机号");
			return map;
		}
		String vcodeKey = PhoneValidationCodeController.PHONE_VALIDATION_CODE_REDIS_KEY_PRE.concat(phone);
		String redisPCode = this.redisService.get(vcodeKey);
		if (StringUtils.isEmpty(smscode) || null == redisPCode || !redisPCode.equalsIgnoreCase(smscode)) {
			map.put("state", "fail");
			map.put("msg", "短信验证码错误");
			return map;
		}
		this.redisService.del(vcodeKey);

		// 获取用户信息
		WxFansUser wxFansUser = (WxFansUser) session.getAttribute(Constant.CURRENT_USER_FANINFO_FROM_EVENT_VIEW);
		AxdCustInfoExample example = new AxdCustInfoExample();
		example.createCriteria().andFanUserIdEqualTo(wxFansUser.getId()).andBandingStatusEqualTo("1");
		PageHelper.startPage(1, 1);
		List<AxdCustInfo> list = axdCustInfoService.list(example);
		if (list.size() > 0) {
			AxdCustInfo axdCustInfo = list.get(0);
			if (axdCustInfo.getExamineStatus().equals("1")) {
				map.put("state", "fail");
				map.put("msg", "您已审核通过，请勿重复提交!");
				return map;
			} else if (axdCustInfo.getExamineStatus().equals("0")) {
				map.put("state", "fail");
				map.put("msg", "信息审核中，请勿重复提交!");
				return map;
			} else {
				axdCustInfo.setCustIdNum(cradId);
				axdCustInfo.setCustPhone(phone);
				axdCustInfo.setCustName(custName);
				axdCustInfo.setExamineStatus("0");
				axdCustInfo.setCreateTime(new Date());
				axdCustInfo.setOpenid(wxFansUser.getOpenid());
				axdCustInfoService.updateById(axdCustInfo);
				map.put("state", "success");
				return map;
			}
		}
		example.clear();
		example.createCriteria().andFanUserIdEqualTo(wxFansUser.getId()).andBandingStatusEqualTo("0");
		PageHelper.startPage(1, 1);
		List<AxdCustInfo> custs = axdCustInfoService.list(example);
		if (custs.size() == 0) {
			AxdCustInfo axdCustInfo = new AxdCustInfo();
			axdCustInfo.setCustIdNum(cradId);
			axdCustInfo.setCustPhone(phone);
			axdCustInfo.setCustName(custName);
			axdCustInfo.setExamineStatus("0");
			axdCustInfo.setBandingStatus("1");
			axdCustInfo.setCreateTime(new Date());
			axdCustInfo.setFanUserId(wxFansUser.getId());
			axdCustInfoService.addOne(axdCustInfo);
			map.put("state", "success");
			return map;
		} else {
			AxdCustInfo axdCustInfo = custs.get(0);
			axdCustInfo.setCustIdNum(cradId);
			axdCustInfo.setCustPhone(phone);
			axdCustInfo.setCustName(custName);
			axdCustInfo.setExamineStatus("0");
			axdCustInfo.setBandingStatus("1");
			axdCustInfo.setCreateTime(new Date());
			axdCustInfoService.updateById(axdCustInfo);
		}
		map.put("state", "success");
		return map;

	}

	@RequestMapping(value = "/card", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> card(String card) {
		Map<String, Object> map = new HashMap<>();
		map.put("idCard", card);
		map.put("token", PropertiesUtils.getConfig(ConfigConstants.IDCRAD_TOKEN));
		String respon = HttpClientUtils.post(map, "http://dbserver.intellbank.com/api/rest/check/idCard");
		@SuppressWarnings("unchecked")
		Map<String, Object> parse = (Map<String, Object>) JSONObject.parse(respon);
		return parse;

	}

	private String makeView(String path) {
		return BASEPATH.concat(path);
	}
}
